<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;

class DiscountCouponPostRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 获取应用于该请求的验证规则。
     *
     * @return array
     */
    public function rules()
    {
        return [
            'title' => 'required|max:50', // 优惠券名称
            'integral' => 'required|integer', // 兑换消耗积分值
            'coupon_price' => 'required', //兑换的优惠券面值
            'use_min_price' => 'required', //最低消费多少金额可用优惠券
            'coupon_time' => 'required|integer', //优惠券有效期限
            'product_id' => 'required', //所属商品id
//            'category_id' => 'required', //分类id
            'sort' => 'required|integer', //排序
//            'status' => 'required|integer', //状态
            'type' => 'required|integer', //优惠券类型
        ];
    }

    /**
     * 获取已定义验证规则的错误消息。
     *
     * @return array
     */
    public function messages()
    {
        return [
            'title.required' => '优惠券必填',
            'title.max' => '优惠券名称过长',
            'integral.required' => '兑换消耗积分值必填',
            'integral.integer' => '兑换消耗积分值必须整数',
            'coupon_price.required' => '兑换的优惠券面值必填',
            'use_min_price.required' => '最低消费多少金额可用优惠券必填',
            'coupon_time.required' => '优惠券有效期限必填',
            'coupon_time.integer' => '优惠券有效期限必须整数',
            'product_id.required' => '所属商品必填',
            'category_id.required' => '所属分类必填',
            'sort.required' => '排序必填',
            'sort.integer' => '排序必须整数',
            'status.required' => '状态必选',
            'status.integer' => '状态必须整数',
            'type.integer' => '优惠券类型必须整数',
            'type.required' => '优惠券类型必须整数',
        ];
    }

    public function failedValidation(Validator $validator)
    {
        throw (new HttpResponseException(response()->json([
            'code' => 20000,
            'msg' => $validator->errors()->first(),
            'data' => $validator->errors()->first(),
        ])));
    }

}
